package com.whu.exp1jsp.controller;

import com.whu.exp1jsp.dao.PurchaseDAO;
import com.whu.exp1jsp.pojo.Contact;
import com.whu.exp1jsp.pojo.ProductSt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "contactServlet", value = "/contact-servlet")
public class ContactServlet extends HttpServlet{

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("mode", 2);
        HttpSession session = request.getSession();
        PurchaseDAO purchaseDAO = new PurchaseDAO();
        Long productId = Long.parseLong(request.getParameter("productId"));
        if(productId == null){
            request.setAttribute("errorMessage", "商品不存在");
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }
        try {
            Contact contact = purchaseDAO.getContact(productId);
            if(contact == null){
                request.setAttribute("errorMessage", "商品不存在");
                request.getRequestDispatcher("index.jsp").forward(request, response);
                return;
            }
            request.setAttribute("successMessage", "卖家联系方式："+contact.getOwnerPhone()+"，买家联系方式："+contact.getCustomerPhone());
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }catch (SQLException e){
            e.printStackTrace();
            request.setAttribute("errorMessage", "数据库错误");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }
}
